package com.wenc.psi.atvt;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.wenc.psi.R;
import com.wenc.psi.databinding.ActivityLoginBinding;
import com.wenc.psi.pojo.Employee;
import com.wenc.psi.vmd.PersonViewModel;

import java.util.concurrent.FutureTask;

public class LoginActivity extends AppCompatActivity{

    private ActivityLoginBinding binding;
    private PersonViewModel employeeViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取DataBinding
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        binding.setLifecycleOwner(this);
        // 获取ViewModle
        employeeViewModel = new ViewModelProvider(this).get(PersonViewModel.class);
        // 对数据监听
        employeeViewModel.getEmployeeLive().observe(this, employee -> {
            // 创建意图
            Intent intent = new Intent(this, HomeActivity.class);
            // 放入数据
            intent.putExtra("employeeId", employee.getId());
            // 开启活动
            startActivity(intent);
        });
    }

    // 点击登录事件
    public void login(View v) {
        // 获取输入框字符串
        String user = binding.etUser.getText().toString().trim();
        String pswd = binding.etPswd.getText().toString().trim();
        // 开启线程执行登录方法
        FutureTask<Employee> futureTask = new FutureTask<>(() -> employeeViewModel.login(user, pswd));
        new Thread(futureTask).start();
        try {
            // 获取返回值
            Employee employee = futureTask.get();
            System.out.println(employee);
            if (employee != null) {
                // 拿到员工信息
                employeeViewModel.getEmployeeById(employee.getId());
                System.out.println(employeeViewModel.hashCode());
            } else {
                Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 点击更多
    public void clickMore(View v){

    }
    // 点击注册
    public void clickRegister(View v){
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }
    // 点击找回
    public void clickFind(View v){

    }
}